<?php
class Db{
    private $host;
    private $root;
    private $port;
    private $pwd;
    private $database;
    public $conn;
    public $sql;
    public function __construct($host,$root,$pwd,$port,$database)
    {
        $this->host=isset($host)?$host :"localhost";
        $this->root=isset($root)?$root:"root";
        $this->port=isset($port)?$port:"80";
        $this->pwd=$pwd;
        $this->database=$database;
        $this->connect();
    }
    // 链接数据库
    public function connect(){
        $this->conn=mysqli_connect($this->host,$this->root,$this->pwd,$this->database);
        if($this->conn){
            // 检测数据库是否链接成功
            echo "数据库链接成功";
        }else{
            echo "数据库链接失败".mysqli_error($this->conn);
            return;
        }
    }
    public function add($name){
        $sql="insert into db1(name) values('".$name."')";
       
        if(mysqli_query($this->conn,$sql)){
            echo "插入成功";
        }else{
            echo "插入失败".mysqli_error($this->conn);
        }
    }
    public function select($id){
        $sql="select * from db1 where id=".$id;
        if(mysqli_query($this->conn,$sql)){
            $result=mysqli_query($this->conn,$sql);
            $result=mysqli_fetch_assoc($result);
            var_dump($result);
        }else{
            echo "查询失败".mysqli_error($this->conn);
        }
    }
    public function update($id,$name){
        $sql="update db1 set name='".$name."'where id=".$id;
        echo $sql;
        if(mysqli_query($this->conn,$sql)){
            echo "修改成功";
        }else{
            echo "修改失败".mysqli_error($this->conn);
        }
    }
    public function del($id){
        $sql="delete from db1 where id=".$id;
        if(mysqli_query($this->conn,$sql)){
            echo "删除成功";
        }else{
            echo "删除失败".mysqli_error($this->conn);
        }
    }
}
$db=new Db('localhost','root','root','8080','db');
// var_dump($db);
// $db->add('想ds哭了');
$db->select(8);
// $db->update(2,'别这样');
// $db->del(9);
?>